package com.school.graduation.goods.presenter;

import android.content.Context;
import android.os.Bundle;

import com.school.graduation.bean.GoodsBean;
import com.school.graduation.goods.model.GetModelListener;
import com.school.graduation.goods.model.GoodsFragmentModel;
import com.school.graduation.goods.model.HotMarketItem;
import com.school.graduation.goods.model.ImplGoodsFragmentModel;
import com.school.graduation.goods.view.FoundGoodDetailsActivityNew;
import com.school.graduation.goods.view.ImplGoodsFragment;
import com.school.graduation.model.Goods;
import com.school.graduation.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class GoodsFragmentPresenter implements GetModelListener
        ,HotMarketItemClickListener{

    private Context context;
    private ImplGoodsFragment goodsFragment;
    private ImplGoodsFragmentModel goodsFragmentModel;
    private int skip = 10;

    public GoodsFragmentPresenter(Context context, ImplGoodsFragment goodsFragment) {
        this.context = context;
        this.goodsFragment = goodsFragment;
        goodsFragmentModel = GoodsFragmentModel.getInstance();
    }

    public void getPicRotationFromBmob() {
        List<String> urls = new ArrayList<>();
        urls.add("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1494245016&di=7517c39837f961ac2bf9e88ec5bbb9a0&src=http://pic.58pic.com/58pic/15/14/95/03858PICmDk_1024.jpg");
        urls.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1494255093965&di=4e269dbe38390e85e909015aaae1ef61&imgtype=0&src=http%3A%2F%2Fimg15.3lian.com%2F2015%2Fa1%2F14%2Fd%2F52.jpg");
        RollPagerAdapter adapter = new RollPagerAdapter(urls);
        goodsFragment.setPicRotationAdapter(adapter);
    }

    public void getHotMarket(){
        goodsFragmentModel.getHotMarkets(this);
    }

    @Override
    public void getUrls(List<String> urls) {
        RollPagerAdapter adapter = new RollPagerAdapter(urls);
        goodsFragment.setPicRotationAdapter(adapter);
    }

    @Override
    public void getHots(List<HotMarketItem> hots) {
        HotMarketAdapter adapter = new HotMarketAdapter(this,hots);
        goodsFragment.setHotMarketAdapter(adapter);
    }


    @Override
    public void getError(String error) {
        goodsFragment.dismissProgressDialog();
        ToastUtil.makeShortToast(context,error);
    }

    @Override
    public void getGoods(List<GoodsBean> goodsBeanList) {
        goodsFragment.dismissProgressDialog();
        RecommendRecyclerAdapterNew adapter= new RecommendRecyclerAdapterNew(goodsBeanList);
        //goodsFragment.setRecommendAdapter(adapter);
        goodsFragment.setRecommendAdapterNew(adapter);
    }

    /*
    6个热门市场的item的点击事件
     */
    @Override
    public void clickFirstHotItem() {
        Bundle bundle = new Bundle();
        bundle.putString("item",getTitle(0));
        bundle.putBoolean("hot",true);
        FoundGoodDetailsActivityNew.toDetail(context,bundle);
    }

    @Override
    public void clickSecondHotItem() {
        Bundle bundle = new Bundle();
        bundle.putString("item",getTitle(1));
        bundle.putBoolean("hot",true);
        FoundGoodDetailsActivityNew.toDetail(context,bundle);
    }

    @Override
    public void clickThirdHotItem() {
        Bundle bundle = new Bundle();
        bundle.putString("item",getTitle(2));
        bundle.putBoolean("hot",true);
        FoundGoodDetailsActivityNew.toDetail(context,bundle);
    }

    @Override
    public void clickFourthHotItem() {
        Bundle bundle = new Bundle();
        bundle.putString("item",getTitle(3));
        bundle.putBoolean("hot",true);
        FoundGoodDetailsActivityNew.toDetail(context,bundle);
    }

    @Override
    public void clickFifthHotItem() {
        Bundle bundle = new Bundle();
        bundle.putString("item",getTitle(4));
        bundle.putBoolean("hot",true);
        FoundGoodDetailsActivityNew.toDetail(context,bundle);
    }

    @Override
    public void clickSixthHotItem() {
        Bundle bundle = new Bundle();
        bundle.putString("item",getTitle(5));
        bundle.putBoolean("hot",true);
        FoundGoodDetailsActivityNew.toDetail(context,bundle);
    }

    private String getTitle(int position){
        return goodsFragmentModel.getHots().get(position).getTitle();
    }

    /**
     * 修改后获取展示信息的方法
     */
    public void getAllMsgFromNet() {
        goodsFragment.showProgressDialog();
        //goodsFragmentModel.getRecommendsFromBmob(this);
        goodsFragmentModel.getAllMsgFromNet(this);
    }
}
